package com.qidian.mes.tm.service.Impl;


import com.qidian.mes.pojo.Dict;
import com.qidian.mes.pojo.ToolType;
import com.qidian.mes.tm.mapper.ToolTypeMapper;
import com.qidian.mes.tm.service.ToolTypeService;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 陆雯城
 * @Description
 * @Time 2024-04-07 18:50
 */
@Service
public class ToolTypeServiceImpl implements ToolTypeService {
    @Autowired
    private ToolTypeMapper toolTypeMapper;


    @Override
    public QdResult queryToolTypeNames() {
        return QdResult.success(toolTypeMapper.selectToolTypeNames());
    }

    @Override
    public QdResult deleteByToolTypeIds(Long[] toolTypeIds) {
        toolTypeMapper.deleteByToolTypeIds(toolTypeIds);
        return QdResult.success();
    }

    @Override
    public QdResult deleteByToolTypeId(Long toolTypeId) {
        toolTypeMapper.deleteByToolTypeId(toolTypeId);
        return QdResult.success();
    }

    @Override
    public QdResult updateToolType(ToolType toolType) {
        if (toolType.getToolTypeId() == null){
            return QdResult.error("ToolTypeId is null");
        }
        ToolType tempToolType = toolTypeMapper.selectByToolTypeCode(toolType.getToolTypeCode());
        if (tempToolType != null && !tempToolType.getToolTypeId().equals(toolType.getToolTypeId())){
            return QdResult.duplicate("duplicate: toolTypeCode");
        }
        tempToolType = toolTypeMapper.selectByToolTypeName(toolType.getToolTypeName());
        if (tempToolType != null && !tempToolType.getToolTypeId().equals(toolType.getToolTypeId())){
            return QdResult.duplicate("duplicate: toolTypeName");
        }
        return  QdResult.isSuccess(toolTypeMapper.updateToolType(toolType));
    }

    @Override
    public QdResult queryByToolTypeId(Long toolTypeId) {
        return QdResult.success(toolTypeMapper.selectByToolTypeId(toolTypeId));
    }

    @Override
    public QdResult saveToolType(ToolType toolType) {
        if (toolTypeMapper.selectByToolTypeName(toolType.getToolTypeName()) != null) {
            return QdResult.duplicate("duplicate: toolTypeName");
        }
        if (toolTypeMapper.selectByToolTypeCode(toolType.getToolTypeCode()) != null)
            return QdResult.duplicate("duplicate: toolTypeCode");
        return QdResult.isSuccess(toolTypeMapper.insertToolType(toolType));
    }

    @Override
    public QdResult<PagerDTO<ToolType>> queryByPage(ToolType toolType, int page, int limit) {
        PagerDTO<ToolType> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(page);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(toolTypeMapper.selectCount(toolType));
        pagerDTO.setData(toolTypeMapper.selectByPage(toolType, (page - 1) * limit, limit));
        return QdResult.success(pagerDTO);
    }

    @Override
    public QdResult<List<ToolType>> queryAll() {
        return QdResult.success(toolTypeMapper.selectAll());
    }
}
